package webgamemaker.Editor;

import webgamemaker.Common.Callback;
import webgamemaker.Common.MapData;
import webgamemaker.Common.TileSet;
import webgamemaker.Editor.HistoryManager.HistoryAction;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.PickerIcon;
import com.smartgwt.client.widgets.form.fields.SpinnerItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.events.FormItemClickHandler;
import com.smartgwt.client.widgets.form.fields.events.FormItemIconClickEvent;

public class CreateTileSetWindow extends Window
{
	public CreateTileSetWindow(final MapData map)
	{
		setTitle("create tileset");
		setIsModal(true);
		setShowModalMask(true);
		centerInPage();
		setWidth(300);  
		setHeight(200); 
		DynamicForm form = new DynamicForm();  
		form.setHeight100();  
		form.setWidth100();  
		form.setPadding(5);  
		form.setLayoutAlign(VerticalAlignment.BOTTOM);  
		final SpinnerItem widthSpinner = new SpinnerItem();
		widthSpinner.setTitle("tile width");
		final SpinnerItem heightSpinner = new SpinnerItem();
		heightSpinner.setTitle("tile height");
		CheckboxItem fromImageItem = new CheckboxItem();  
		fromImageItem.setName("fromImage");  
		fromImageItem.setTitle("from Image");  
		fromImageItem.setValue(false);  
		
		final TextItem imageItem = new TextItem("imageLoader", "image"); 
		PickerIcon imagePicker = new PickerIcon(PickerIcon.SEARCH, new FormItemClickHandler() 
			{
				public void onFormItemClick(FormItemIconClickEvent event) 
				{
					final FileNavigatorWindow win = new FileNavigatorWindow(".");
					win.addCallback(new Callback()
						{
							public void onCall(Object src) 
							{
								imageItem.setValue(win.selectedFile);
							}
						});
					win.show();
				}  
			});    
		imageItem.setIcons(imagePicker);  
		imageItem.setDisabled(true);
		fromImageItem.addChangeHandler(new ChangeHandler() 
			{  
				public void onChange(ChangeEvent event) 
				{  
					imageItem.setDisabled(!((Boolean) event.getValue()));  
				}  
			});  
		ButtonItem okButton = new ButtonItem("ok");  
		okButton.setTitle("ok");
		okButton.setAlign(Alignment.RIGHT);
		okButton.addClickHandler(new ClickHandler()
			{
				public void onClick(ClickEvent event) 
				{
					String widthStr = widthSpinner.getDisplayValue();
					String heightStr = heightSpinner.getDisplayValue();
					int width = 0, height = 0;
					try{ width = Integer.parseInt(widthStr); }catch(NumberFormatException e){}
					try{ height = Integer.parseInt(heightStr); }catch(NumberFormatException e){}
					final TileSet tileset;
					if(imageItem.isDisabled())
						tileset = new TileSet(width, height);
					else tileset = new TileSet(ProjectManager.getInstance().data.rootDir+"/"+imageItem.getDisplayValue(), width, height);
					ProjectManager.getInstance().data.historyManager.makeAction(new HistoryAction()
					{
						public void redo() 
						{
							ProjectManager.getInstance().addTileSet(map, tileset);
						}
						public void undo() 
						{
							ProjectManager.getInstance().removeTileSet(map, tileset);
						}
					
					});
					destroy();
				}
			});
		form.setFields(widthSpinner, heightSpinner, fromImageItem, imageItem, okButton);
		addItem(form);
	}
}
